<template>
  <div>
    <div class="list" ref="wrapper">
      <div>

        <div class="area">
          <div class="title border-bottom">当前城市</div>
          <div class="btn-list">
            <div class="wrapper">
              <div class="btn">{{this.currentCity}}</div>
            </div>
          </div>
        </div>

        <div class="area">
          <div class="title border-bottom">热门城市</div>
          <div class="btn-list">
            <div class="wrapper" 
            v-for="item of hot" 
            :key="item.id"
            @click="cityclick(item.name)"
            >
              <div class="btn">{{item.name}}</div>
            </div>
          </div>
        </div>

        <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
          <div class="title border-bottom">{{key}}</div>
          <div class="item-list">
            <div class="item border-bottom" 
            v-for="i of item" 
            :key="i.id"
            @click="cityclick(i.name)"
            >
              {{i.name}}

            </div>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import {mapState, mapMutations} from 'vuex'
import scroll from "better-scroll";
export default {
  name: "CityList",
  props: {
    hot: Array,
    cities: Object,
    letter: String
  },
  updated() {
    this.bscroll = new scroll(this.$refs.wrapper);
  },
  watch: {
    letter() {
      // console.log(this.letter);
      if(this.letter) {
        const element = this.$refs[this.letter][0]
        this.bscroll.scrollToElement(element)
      }
    }
  },
  methods: {
    cityclick(newcity) {
      // this.$store.commit('changecity', newcity)
      this.changecity(newcity)
      this.$router.push('/')
    },
    ...mapMutations(['changecity'])
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  }
};
</script>

<style scoped>
.list {
  overflow: hidden;
  position: absolute;
  top: 1.68rem;
  left: 0;
  right: 0;
  bottom: 0;
}
.title {
  padding-left: 0.1rem;
  height: 0.4rem;
  line-height: 0.4rem;
  background: #ddd;
}
.btn-list {
  overflow: hidden;
  padding: 0.1rem 0.6rem 0.1rem 0.1rem;
}
.wrapper {
  float: left;
  width: 33.33%;
}
.btn {
  margin: 0.1rem;
  padding: 0.1rem;
  text-align: center;
  border: 0.02rem solid #ccc;
  border-radius: 0.1rem;
}
.item {
  padding-left: 0.2rem;
  line-height: 0.5rem;
}
</style>